package com.baibei.ebec.user.auth;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.ebec.user.register.TextViewCompat;
import com.baibei.model.event.UpdateUserInfoEvent;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;
import com.baibei.sdk.RxObservable;
import com.baibei.ui.AppUI;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Action;

/**
 * 实名认证
 * Created by ChenRui on 2017/8/16 0016 16:44.
 */
@Route(path = AppRouter.ROUTER_NAME_AUTH)
public class NameAuthActivity extends BasicActivity {

    @BindView(R2.id.et_id_card)
    EditText mIdCard;
    @BindView(R2.id.et_name)
    EditText mName;
    @BindView(R2.id.btn_done)
    Button mDoneButton;
    private TextWatcher mTextWatcher;

    private IUserApi mUserApi;
    private String mTag = "nameAuth";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_auth);
        mUserApi = ApiFactory.getInstance().getUserApi();
        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence text, int i, int i1, int i2) {
                if (mIdCard.length() >= 18 && mName.length() > 1) {
                    mDoneButton.setEnabled(true);
                } else {
                    mDoneButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
        TextViewCompat.registerFormListener(mTextWatcher, mIdCard, mName);
    }

    @Override
    protected void onDestroy() {
        TextViewCompat.unregisterFormListener(mTextWatcher, mIdCard, mName);
        RxObservable.dispose(mTag);
        super.onDestroy();
    }

    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }

    public String getIdCard() {
        return mIdCard.getText().toString();
    }

    public String getName() {
        return mName.getText().toString();
    }

    @OnClick(R2.id.btn_done)
    public void onDoneClick() {
        if (TextUtils.isEmpty(getIdCard())) {
            AppUI.failed(this, mIdCard.getHint().toString());
            return;
        }
        if (TextUtils.isEmpty(getName())) {
            AppUI.failed(this, mName.getHint().toString());
            return;
        }

        AppUI.loading(this, "正在提交认证信息");
        RxObservable.create(mUserApi.realNameIdentify(getName(), getIdCard()), mTag)
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        AppUI.dismiss();
                    }
                })
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String s) {
                        AppUI.failed(getContext(), s);
                    }

                    @Override
                    protected void accept(Empty balanceInfo) {
                        EventBus.getDefault().post(new UpdateUserInfoEvent());

                        AppRouter.routeToNameAuthStatus(getContext(), getName(), getIdCard());
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }


}
